package gui;

//import TDAListaDE.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import juego.Logica;

public class Nivel extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanelConFondo fondo;
	private int anchoCasillero;
	private int[] techo;
	private ThreadCrearEnemigo tce;
	private ThreadMoverEnemigo tme;
	private Logica logicaJuego;
	private Perdiste p;
	
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Nivel frame = new Nivel();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Nivel() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(50, 50, 1000,700);
		p = new Perdiste(this.getWidth()/2 - 100,this.getHeight()/2 - 100);
		techo = new int[5];
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(1, 0, 0, 0));
		
		fondo = new JPanelConFondo("/imagenes/fondo.jpg");
		contentPane.add(fondo);
		fondo.setLayout(null);
		inicializarTecho();
		logicaJuego = new Logica();
		anchoCasillero = getWidth()/10;
		iniciarThreads();
	}
	
	private void inicializarTecho()
	{
		int t = getHeight() / 5;
		int tope = getHeight() - t;
		for(int i=4; i >= 0;i--)
		{
			techo[i]=tope;
			tope = tope - t;
		}
	}
	
	public JLabel crearEnemigo(int i, String nombre)
	{
		JLabel enemigo = new JLabel();
		ImageIcon imagen = new ImageIcon(getClass().getClassLoader().getResource(nombre+".gif"));
		enemigo.setBounds(getWidth()-imagen.getIconWidth(),techo[i],imagen.getIconWidth(), 125);
		enemigo.setIcon(imagen);
		fondo.add(enemigo);
		repaint();
		return enemigo;
	}
	
	public void perder()
	{
		p.setGUI(this);
		tce.terminar();
		tme.terminar();
		p.setVisible(true);
	}
	
	public void limpiarPantalla() 
	{
		tme.limpiarPantalla();
		repaint();
	}
	
	public void iniciarThreads()
	{
		tme = new ThreadMoverEnemigo(5, anchoCasillero, logicaJuego, fondo, this);
		tce = new ThreadCrearEnemigo(2000,logicaJuego, this, tme);
		tce.start();
		tme.start();
	}
}
